package com.zhang.servicemap.service.impl;

import com.zhang.internalcommon.constant.AmapConfigConstant;
import com.zhang.internalcommon.constant.CommonStatusEnum;
import com.zhang.internalcommon.dto.DicDistrict;
import com.zhang.internalcommon.dto.ResponseResult;
import com.zhang.servicemap.mapper.DicDistrictMapper;
import com.zhang.servicemap.remote.MapDicDistrictClient;
import com.zhang.servicemap.service.DicDistrictService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * 类功能描述
 *
 * @author Kye
 * @date 2025-04-11
 */
@Service
public class DicDistrictServiceImpl implements DicDistrictService {

	@Autowired
	private MapDicDistrictClient mapDicDistrictClient;

	@Autowired
	private DicDistrictMapper dicDistrictMapper;

	/**
	 * 解析行政区域信息插入到数据库
	 * @param keywords
	 * @return
	 */
	@Override
	public ResponseResult initDicDistrict(String keywords) {
		// 请求地图
		String dicDistrictResult = mapDicDistrictClient.dicDistrict(keywords);
		// System.out.println(dicDistrict);
		// 解析结果
		JSONObject dicDistrictJson = JSONObject.fromObject(dicDistrictResult);
		int status = dicDistrictJson.getInt(AmapConfigConstant.STATUS);
		if (status != 1) {
			return ResponseResult.fail(CommonStatusEnum.MAP_DISTRICT_ERROR.getCode(), CommonStatusEnum.MAP_DISTRICT_ERROR.getValue());
		}
		JSONArray countryJsonArray = dicDistrictJson.getJSONArray(AmapConfigConstant.DISTRICTS);

		// 解析行政区域信息并插入数据库
		for (int i = 0; i < countryJsonArray.size(); i++) {
			// 解析国家信息
			JSONObject countryjsonObject = countryJsonArray.getJSONObject(i);
			String addressCode = countryjsonObject.getString(AmapConfigConstant.ADCODE);
			String addressName = countryjsonObject.getString(AmapConfigConstant.NAME);
			String parentAddressCode = "0";
			String level = countryjsonObject.getString(AmapConfigConstant.LEVEL);

			insertDicDistrict(addressCode, addressName, parentAddressCode, level);

			// 解析省级区域信息
			JSONArray proviceJsonArray = countryjsonObject.getJSONArray(AmapConfigConstant.DISTRICTS);
			for (int j = 0; j < proviceJsonArray.size(); j++) {
				JSONObject provicejsonObject = proviceJsonArray.getJSONObject(j);
				String proviceAddressCode = provicejsonObject.getString(AmapConfigConstant.ADCODE);
				String proviceAddressName = provicejsonObject.getString(AmapConfigConstant.NAME);
				String proviceParentAddressCode = addressCode;
				String provicelevel = provicejsonObject.getString(AmapConfigConstant.LEVEL);

				insertDicDistrict(proviceAddressCode, proviceAddressName, proviceParentAddressCode, provicelevel);

				// 解析市级区域信息
				JSONArray cityjsonArray = provicejsonObject.getJSONArray(AmapConfigConstant.DISTRICTS);
				for (int k = 0; k < cityjsonArray.size(); k++) {
					JSONObject cityjsonObject = cityjsonArray.getJSONObject(k);
					String cityCode = cityjsonObject.getString(AmapConfigConstant.ADCODE);
					String cityName = cityjsonObject.getString(AmapConfigConstant.NAME);
					String cityParentAddressCode = proviceAddressCode;
					String citylevel = cityjsonObject.getString(AmapConfigConstant.LEVEL);

					insertDicDistrict(cityCode, cityName, cityParentAddressCode, citylevel);

					// 解析区信息
					if ("130100".equals(cityCode)){
						System.out.println(citylevel);
					}
					JSONArray districtJsonArray = cityjsonObject.getJSONArray(AmapConfigConstant.DISTRICTS);
					for (int l = 0; l < districtJsonArray.size(); l++) {
						JSONObject districtjsonObject = districtJsonArray.getJSONObject(l);
						String districtCode = districtjsonObject.getString(AmapConfigConstant.ADCODE);
						String districtName = districtjsonObject.getString(AmapConfigConstant.NAME);
						String districtParentAddressCode = cityCode;
						String districtlevel = districtjsonObject.getString(AmapConfigConstant.LEVEL);

						// 判断等级是否属于街道
						if (districtlevel.equals(AmapConfigConstant.STREET)){
							continue;
						}

						insertDicDistrict(districtCode, districtName, districtParentAddressCode, districtlevel);
					}
				}
			}
		}

		return ResponseResult.success();
	}

	/**
	 * 插入数据库
	 * @param addressCode 地区码
	 * @param addressName 地区名
	 * @param parentAddressCode 父级地区码
	 * @param level 等级
	 */
	public void insertDicDistrict(String addressCode, String addressName, String parentAddressCode, String level) {
		// 数据库对象
		DicDistrict dicDistrict = new DicDistrict();
		dicDistrict.setAddressCode(addressCode);
		dicDistrict.setAddressName(addressName);
		dicDistrict.setParentAddressCode(parentAddressCode);
		int levelInt = generateLevel(level);
		dicDistrict.setLevel(levelInt);

		dicDistrictMapper.insert(dicDistrict);
	}

	/**
	 * 解析等级
	 * @param level 等级
	 * @return
	 */
	public int generateLevel(String level) {
		int levelInt = 0;
		if ("country".equals(level.trim())) {
			levelInt = 0;
		} else if ("province".equals(level.trim())) {
			levelInt = 1;
		} else if ("city".equals(level.trim())) {
			levelInt = 2;
		} else if ("district".equals(level.trim())) {
			levelInt = 3;
		}
		return levelInt;
	}
}
